const express = require('express')
//认证方式  用户登录   前端   权限
const jwt = require("jsonwebtoken")
const router = express.Router()
const pool = require('../src/pg/config/pg')
router.post("/login", async(req,res,next)=>{
    const username = req.body.username
    const password =req.body.password
    console.log(username , password)
    await pool.query("SELECT * FROM usinfo  ", (err, resq) => {
        if (err) {
          return res.status(200).json({errinfo: "提交用户和密码密码在后端报错" });
          pool.end;
        }else{
              const data = resq.rows
        const userinfo=     data.find(itme=>itme.username===username )
        console.log(userinfo)
        if(!userinfo){
            res.status(200).json({message:"用户名不对 ",id:"用户名不对"})
            console.log("用户名不对")
            pool.end;
        } else{
          const pwd=  data.find(item=>item.password===password)
          if(!pwd){
            res.status(200).json({message:"密码不对 ",id:"密码不对"})
            console.log("密码不对")
            pool.end;
          }else{
           
   const token   =     jwt.sign({id:"1"},"xiaolu001")
   if(token){
    res.status(200).json({
      id:"登录成功",
        message:"登录成功",
        token:token
    })
           console.log("登录成功")
           pool.end;
   }
          }
        }
        }
       
      });
})
module.exports = router